package ciyuan.Network;

import ciyuan.Player.AttrbuteService;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;

import java.util.function.Supplier;

public class AddAttrbutePacket {

    private final String attrbuteName;

    public AddAttrbutePacket(PacketBuffer buffer) {
        attrbuteName = buffer.readUtf();
    }

    public AddAttrbutePacket(String attrbuteName) {
        this.attrbuteName = attrbuteName;
    }

    public void toBytes(PacketBuffer buf) {
        buf.writeUtf(attrbuteName);
    }

    public void handler(Supplier<NetworkEvent.Context> ctx) {
        if (ctx.get().getDirection().getOriginationSide().isClient()) {
            ctx.get().enqueueWork(() -> {
                AttrbuteService.tryAddAttribute(ctx.get().getSender(), attrbuteName);
            });
        }
        ctx.get().setPacketHandled(true);
    }
}